package com.example.ysdemo.po;

import lombok.Data;

/**
 * @describe 统一响应类
 * @date 2020/9/21
 */
@Data
public class ResultVo<T> {

    /**
     * 结果码
     */
    private int code;

    /**
     * 结果消息
     */
    private String msg;

    /**
     * 结果数据载体
     */
    private T data;

    /**
     * 是否响应成功
     */
    private boolean success;

    /**
     * en:英文 zh:中文
     */
    private String lang ;

    /**
     * 私有构造
     */
    private ResultVo(int code, String message, boolean success) {
        this.code = code;
        this.msg = message;
        this.success = success;
    }

    private ResultVo(int code, String message, boolean success, String lang) {
        this.code = code;
        this.msg = message;
        this.success = success;
        this.lang = lang;
    }

    private ResultVo() {
    }

    /**
     * 填充数据
     */
    public ResultVo<T> put(T data) {
        this.data = data;
        return this;
    }

    public ResultVo<T> put(T data, String lang) {
        this.data = data;
        this.lang = lang;
        return this;
    }

    public ResultVo<T> put(String lang) {
        this.lang = lang;
        return this;
    }

    public static ResultVo ok() {
        return new ResultVo(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),true);
    }

    public static ResultVo ok(String msg) {
        return new ResultVo(ResultCode.SUCCESS.getCode(), msg,true);
    }

    public static ResultVo fail() {
        return new ResultVo(ResultCode.ERROR.getCode(), ResultCode.ERROR.getMessage(),false);
    }

    public static ResultVo fail(String msg) {
        return new ResultVo(ResultCode.ERROR.getCode(), msg,false);
    }

    public static ResultVo fail(ResultCode resultCode) {
        return new ResultVo(resultCode.getCode(), resultCode.getMessage(),false);
    }

    public static ResultVo fail(Exception exception) {
        return new ResultVo(ResultCode.ERROR.getCode(), null != exception ? exception.getMessage() : ResultCode.ERROR.getMessage(),false);
    }

    public static ResultVo makeFail(String message) {
        return makeResponse(1, message, (Object) null);
    }

    public static ResultVo makeSuccess(Object obj) {
        return makeResponse(0, "", obj);
    }

    public static ResultVo makeSuccess(Object obj, String msg) {
        return makeResponse(0, msg, obj);
    }

    public static ResultVo makeFail(Object obj) {
        return makeResponse(1, "", obj);
    }

    public static ResultVo makeError(ResultCode errorCode) {
        return makeResponse(errorCode.getCode(), errorCode.getMessage(), (Object) null);
    }

    public static ResultVo makeError(ResultCode errorCode, Object obj) {
        return makeResponse(errorCode.getCode(), errorCode.getMessage(), obj);
    }

    public static ResultVo makeAdminError(ResultCode errorCode) {
        return makeResponse(errorCode.getCode(), errorCode.getMessage(), (Object) null);
    }

    public static ResultVo makeAdminError(ResultCode errorCode, Object obj) {
        return makeResponse(errorCode.getCode(), errorCode.getMessage(), obj);
    }

    public static ResultVo makeResponse(int code, String msg, Object obj) {
        ResultVo result = new ResultVo();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(obj);
        return result;
    }

    public boolean isOk(ResultVo response) {
        return response != null && response.getCode() == 0;
    }

}
